#!/bin/bash
#
# Tape DSP uninstaller (macOS app executable). GUI-only — no Terminal window.
# Bundled as Uninstall Tape DSP.app, signed + notarized for Gatekeeper.
set -euo pipefail

HERE="$(cd "$(dirname "$0")" && pwd)"
CORE="$HERE/../Resources/uninstall-core.sh"
if [ ! -f "$CORE" ]; then
  osascript -e 'display alert "Uninstaller error" message "A required file is missing from this app bundle." as critical buttons {"OK"}' || true
  exit 1
fi
source "$CORE"

button="$(osascript <<'APPLESCRIPT' || true
display dialog "This removes Tape DSP Machine, Echo and Compressor (VST3 + AU) from this Mac." & return & return & "You may be asked for your administrator password." buttons {"Cancel", "Uninstall"} default button "Cancel" with title "Uninstall Tape DSP" with icon caution
APPLESCRIPT
)"

case "$button" in
  *Uninstall*) ;;
  *) exit 0;;
esac

if ! tape_dsp_remove_system_plugins; then
  osascript -e 'display alert "Uninstall cancelled" message "System plug-ins were not removed." as warning buttons {"OK"} default button "OK"' || true
  exit 1
fi

tape_dsp_remove_user_plugins
tape_dsp_forget_pkg_receipts
tape_dsp_remove_app_folder

osascript <<'APPLESCRIPT' || true
display alert "Tape DSP removed" message "The plug-ins have been removed from this Mac." buttons {"OK"} default button "OK"
APPLESCRIPT
